<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: PortletMan.php 357 2010-02-02 21:43:21Z steen.rabol@gmail.com $
 *
 */

class PortletMan extends PortalAdminPage
{
	public function getDataRows($offset, $rows)
	{
		return $this->DataAccess->createCommand("select * from tblportlets LIMIT $offset,$rows")->query();
	}

	protected function getRowCount()
    {
        return $this->DataAccess->createCommand("select count(id) from tblportlets")->queryScalar();
    }

	public function onLoad($param)
	{
		parent::onLoad($param);

		$this->setAdminPageTitle('Portlets');

		if(!$this->IsPostBack)
		{
			$this->PortletsGrid->VirtualItemCount	= $this->RowCount;
			$this->PortletsGrid->DataSource			= $this->getDataRows(0,$this->PortletsGrid->PageSize);
			$this->PortletsGrid->dataBind();
		}
	}

	public function changePage($sender,$param)
	{
		$this->PortletsGrid->CurrentPageIndex=$param->NewPageIndex;
        $offset=$param->NewPageIndex*$this->PortletsGrid->PageSize;
        $this->PortletsGrid->DataSource=$this->getDataRows($offset,$this->PortletsGrid->PageSize);
        $this->PortletsGrid->dataBind();
	}

	public function pagerCreated($sender,$param)
	{
		$param->Pager->Controls->insertAt(0, Prado::localize('Page').': ');
	}

	public function editItem($sender,$param)
	{
		$this->PortletsGrid->EditItemIndex = $param->Item->ItemIndex;
        $offset = $this->PortletsGrid->CurrentPageIndex * $this->PortletsGrid->PageSize;
        $this->PortletsGrid->DataSource=$this->getDataRows($offset,$this->PortletsGrid->PageSize);
        $this->PortletsGrid->dataBind();
	}

	public function saveItem($sender,$param)
	{
		$item				= $param->Item;
		$Id					= $this->PortletsGrid->DataKeys[$item->ItemIndex];
		$pRecord			= $this->DataAccess->createCommand("select * from tblportlets where id=$Id")->queryRow();
		$pRecord['active']	= TPropertyValue::ensureInteger($item->Cells[2]->PageStatus->SelectedValue);
		$hasadmin			= TPropertyValue::ensureInteger($item->Cells[3]->PageHasAdmin->SelectedValue);
		$displayname		= $item->DisplayName->TextBox->Text;
		$sql				= "update tblportlets set active=" . $pRecord['active'] . ",displayname='$displayname',hasadmin=$hasadmin where id=" . $pRecord['id'];
		$this->DataAccess->createCommand($sql)->execute();
		$this->gotoPage('Admin.PortletMan');
	}

	public function cancelItem($sender,$param)
	{
		$this->gotoPage('Admin.PortletMan');
	}

	public function deleteItem($sender,$param)
	{
		$item	= $param->Item;
		$Id		= $this->PortletsGrid->DataKeys[$item->ItemIndex];

		$p = $this->DataAccess->createCommand("select * from tblportlets where id=$Id")->queryRow();
		// Load the portlet and perform 'un-install'
		$classfile	= $this->Application->BasePath . "/Portlets/" . $p['name'] . "/Install/" . $p['name'] . ".php";
		$classname	= $p['name'];
		Prado::using('Application.Portlets.' . $p['name'] . '.Install.' . $p['name']);
		$portlet	= new $classname;

		if($portlet->PortletUnInstall())
		{
			$sql	= "delete from tblportlets where id=$Id";
			$this->DataAccess->createCommand($sql)->execute();
			$this->gotoPage('Admin.PortletMan');
		}
	}

	public function itemCreated($sender,$param)
	{
		$item=$param->Item;
		if($item->ItemType==='EditItem')
		{
			$item->DisplayName->TextBox->Columns=45;
		}

		if($item->ItemType==='Item' || $item->ItemType==='AlternatingItem' || $item->ItemType==='EditItem')
		{
			// add an aleart dialog to delete buttons
			$item->DeleteColumn->Button->Attributes->onclick='if(!confirm(\'' . Prado::localize("Are you sure?"). '\')) return false;';
		}
	}
}
?>